package com.test.mylotterysystem.service.ActivityStatusChange.Impl;

import com.test.mylotterysystem.comment.errorCode.ServiceErrorCode;
import com.test.mylotterysystem.comment.exception.ServiceException;
import com.test.mylotterysystem.controller.Param.DrawPrizeParam;
import com.test.mylotterysystem.service.ActivityStatusChange.ActivityStatusChange;
import com.test.mylotterysystem.service.ActivityStatusChange.Operator.AbstractOperator;
import com.test.mylotterysystem.service.DTO.ConvertActivityStatusDTO;
import com.test.mylotterysystem.service.serviceEnum.ActivityPrizeStatusEnum;
import com.test.mylotterysystem.service.serviceEnum.ActivityStatusEnum;
import com.test.mylotterysystem.service.serviceEnum.ActivityUserStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ActivityStatusChangeImpl implements ActivityStatusChange {

    @Autowired
    private Map<String, AbstractOperator> map;

    /**
     * 反转状态
     *
     * @param message
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void convertStatus(DrawPrizeParam message) {
        if (map.isEmpty()) {
            log.error("map为空");
            return;
        }

        Map<String, AbstractOperator> curMap = new HashMap<>(map);

        ConvertActivityStatusDTO convertActivityStatusDTO = new ConvertActivityStatusDTO();
        convertActivityStatusDTO.setActivityId(message.getActivityId());
        convertActivityStatusDTO.setTargetActivityStatus(ActivityStatusEnum.FINISH);
        convertActivityStatusDTO.setPrizeId(message.getPrizeId());
        convertActivityStatusDTO.setTargetPrizeStatus(ActivityPrizeStatusEnum.COMPLETE);
        List<Long> userIds = message.getWinnerList().stream().map(DrawPrizeParam.Winner::getUserId).collect(Collectors.toList());
        convertActivityStatusDTO.setUserIds(userIds);
        convertActivityStatusDTO.setTargetUserStatus(ActivityUserStatusEnum.COMPLETE);

        // 转换优先级为1
        processConvertStatus(curMap, 1, convertActivityStatusDTO);

        // 转换优先级为2
        processConvertStatus(curMap, 2, convertActivityStatusDTO);

    }

    /**
     * 转换状态
     *
     * @param curMap
     * @param route
     * @param convertActivityStatusDTO
     */
    private void processConvertStatus(Map<String, AbstractOperator> curMap, int route, ConvertActivityStatusDTO convertActivityStatusDTO) {
        Collection<AbstractOperator> values = curMap.values();
        Iterator<AbstractOperator> iterator = values.iterator();
        while (iterator.hasNext()) {
            AbstractOperator next = iterator.next();
            if (next.sequence() != route || !next.needConvert(convertActivityStatusDTO)) {
                continue;
            }

            if (!next.convert(convertActivityStatusDTO)) {
                log.error("修改数据失败...");
                throw new ServiceException(ServiceErrorCode.CONVERT_ERROR);
            }

            iterator.remove();
        }

    }
}
